package com.example.wto.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
{

    private ListView week_lv=null;
    private ImageView code_imgV=null;
    private AutoCompleteTextView teacher_id_auto_et=null;
    private EditText code_et=null;
    private ParseHtml parseHtml=new ParseHtml();
    private TeacherAdapter adapter=null;
    private UtilSqlHelper sqlHelper=null;
    private Teacher teacher=new Teacher();
    private UtilTeacherSql teacherSql=null;
    private List <TermBean> termList=new ArrayList<>();
    private List<TeacherBean> teacherBeanList=new ArrayList<>();
    private TermAdapter termAdapter=null;
    private Spinner termSpinner=null;
    private TermBean termSelected=null;
    private TeacherBean teacherSelected=null;
    private ArrayAdapter<String> teacherBeanAdapter=null;
    private List<String> teacherNameArray=new ArrayList<>();
    private CatalogAdapter catalogAdapter=null;
    private List<CatalogBean> catalogBeanList=new ArrayList<>();
    private Spinner catalogSpinner=null;
    private LinearLayout bar_list_one_ly=null;
    private LinearLayout bar_list_two_ly=null;
    private LinearLayout bar_list_three_ly=null;
    private LinearLayout bar_list_four_ly=null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        teacher_id_auto_et=(AutoCompleteTextView) findViewById(R.id.teacher_id_edittext);
        code_et=(EditText)findViewById(R.id.code_edittext);
        week_lv=(ListView)findViewById(R.id.teacher_listview);
        code_imgV=(ImageView)findViewById(R.id.code_imagview);
        termSpinner=(Spinner)findViewById(R.id.term_spinner);
        bar_list_one_ly=(LinearLayout)findViewById(R.id.bar_list_one_ly);
        bar_list_two_ly=(LinearLayout)findViewById(R.id.bar_list_two_ly);
        bar_list_three_ly=(LinearLayout)findViewById(R.id.bar_list_three_ly);
        bar_list_four_ly=(LinearLayout)findViewById(R.id.bar_list_four_ly);
        teacher.getWeekList().clear();
        catalogSpinner=(Spinner)findViewById(R.id.catalog_spinner);

        adapter=new TeacherAdapter(this,R.layout.course_layout,teacher.getWeekList());
        week_lv.setAdapter(adapter);
        //学期下拉列表
        termAdapter=new TermAdapter(this,R.layout.iterm_layout,termList);
        termAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        termSpinner.setAdapter(termAdapter);
        //教师姓名自动匹配列表
        teacherNameArray.clear();
        teacherNameArray.addAll(Util.teacherBeanToList(teacherBeanList));
        teacherBeanAdapter=new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,teacherNameArray);
        teacher_id_auto_et.setAdapter(teacherBeanAdapter);
        teacher_id_auto_et.setThreshold(1);
        //目录列表
        catalogBeanList.add(new CatalogBean("教师课表","http://jwgl.lnc.edu.cn/ZNPK/TeacherKBFB.aspx"));
        catalogBeanList.add(new CatalogBean("课程课表","http://jwgl.lnc.edu.cn/ZNPK/KBFB_LessonSel.aspx"));
        catalogBeanList.add(new CatalogBean("班级课表","http://jwgl.lnc.edu.cn/ZNPK/KBFB_ClassSel.aspx"));
        catalogBeanList.add(new CatalogBean("教室课表","http://jwgl.lnc.edu.cn/ZNPK/KBFB_RoomSel.aspx"));
        catalogAdapter=new CatalogAdapter(this,R.layout.catalog_layout,catalogBeanList);
        catalogSpinner.setAdapter(catalogAdapter);
        catalogAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //学期监听事件
        termSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                termSelected=termList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {

            }
        });

        new Thread()
        {
            @Override
            public void run()
            {
                List<TermBean> t=parseHtml.getIndex();
                if(parseHtml.getImage()!=-1)
                {
                    String imgPath=Environment.getExternalStorageDirectory()+File.separator+"codeImage.jpg";
                    final Bitmap bmp= BitmapFactory.decodeFile(imgPath);
                    MainActivity.this.runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            code_imgV.setImageBitmap(bmp);
                        }
                    });

                }
                if(t!=null)
                {

                    termSelected=t.get(0);//初始化显示第一个学期信息
                    termList.clear();
                    termList.addAll(t);
                    MainActivity.this.runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            termAdapter.notifyDataSetChanged();
                        }
                    });
                    List<TeacherBean> tb=parseHtml.getTeachers(termSelected.getTermID());

                    if(tb!=null)
                    {
                        teacherBeanList.clear();
                        teacherBeanList.addAll(tb);
                        teacherNameArray.clear();
                        teacherNameArray.addAll(Util.teacherBeanToList(teacherBeanList));

                        MainActivity.this.runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                teacherBeanAdapter.notifyDataSetChanged();
                            }
                        });

                    }

                }
            }
        }.start();

        //下载验证码

        //创建数据库
        sqlHelper=new UtilSqlHelper(this,"test.db",null,1);
    }

    public void searchOnclick(View view)
    {
        final String semester_id=termSelected.getTermID();
        final String teacherName=teacher_id_auto_et.getText().toString();
        final String code=code_et.getText().toString();
        if(teacherName.equals(""))
        {
            teacher.getWeekList().clear();
            adapter.notifyDataSetChanged();
            Toast toast=Toast.makeText(MainActivity.this,"请输入教师姓名",Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
            return;
        }
        if(teacherNameArray.contains(teacherName)==false)
        {
            teacher.getWeekList().clear();
            adapter.notifyDataSetChanged();
            Toast toast=Toast.makeText(MainActivity.this,"不存在该教师，请重新输入",Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
            return;
        }
        int index=teacherNameArray.indexOf(teacherName);
        final String teacher_id=teacherBeanList.get(index).getTeacherID();
        teacher.setS_id(semester_id);
        teacher.setId(teacher_id);
        teacher.setName(teacherName);
        teacherSql=new UtilTeacherSql(sqlHelper);
        Teacher tempTeacher=teacherSql.select(semester_id,teacher_id);
        //如果数据库中有数据，则用数据库中的数据
        if(tempTeacher!=null)
        {
            teacher.getWeekList().clear();
            teacher.getWeekList().addAll(tempTeacher.getWeekList());
            adapter.notifyDataSetChanged();
            Log.i("tttttt", tempTeacher.toJsonObject());
        }
        else
        {
            new Thread()
            {
                @Override
                public void run()
                {
                    if(code.equals(""))
                    {
                        teacher.getWeekList().clear();
                        MainActivity.this.runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                adapter.notifyDataSetChanged();
                                Toast toast=Toast.makeText(MainActivity.this,"请输入验证码",Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER,0,0);
                                toast.show();
                            }
                        });

                        return;
                    }
                    Teacher t = parseHtml.getTeacher(semester_id, teacher_id, code);
                    if(parseHtml.CODE_ERROR==1)
                    {
                        if(-1!=parseHtml.getImage())
                        {
                            teacher.getWeekList().clear();
                            String imgPath=Environment.getExternalStorageDirectory()+File.separator+"codeImage.jpg";
                            final Bitmap bmp= BitmapFactory.decodeFile(imgPath);
                            MainActivity.this.runOnUiThread(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    Toast toast=Toast.makeText(MainActivity.this,"验证码错误，请重新输入",Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER,0,0);
                                    toast.show();
                                    code_imgV.setImageBitmap(bmp);
                                    adapter.notifyDataSetChanged();
                                }
                            });
                            return;
                        }

                    }
                    if (t != null)
                    {
                        Log.i("teacher", teacher.toJsonObject());

                        teacher.getWeekList().clear();
                        teacher.getWeekList().addAll(t.getWeekList());
                        teacherSql.insert(teacher);
                        MainActivity.this.runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                adapter.notifyDataSetChanged();
                            }
                        });
                    } else
                    {
                        teacher.getWeekList().clear();
                        MainActivity.this.runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                Toast toast=Toast.makeText(MainActivity.this,"未找到这学期该教师的课程",Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER,0,0);
                                toast.show();
                                adapter.notifyDataSetChanged();
                            }
                        });

                    }
                }
            }.start();
        }
    }

    public void barOnclick(View view)
    {
        bar_list_one_ly.setBackgroundColor(Color.parseColor("#FFFFFF"));
        bar_list_two_ly.setBackgroundColor(Color.parseColor("#FFFFFF"));
        bar_list_three_ly.setBackgroundColor(Color.parseColor("#FFFFFF"));
        bar_list_four_ly.setBackgroundColor(Color.parseColor("#FFFFFF"));
        view.setBackgroundColor(Color.parseColor("#83AF9B"));
    }
}
